<?php

!defined('IN_HDWIKI') && exit('Access Denied');

class wordmodel {

	var $db;
	var $base;

	function wordmodel(&$base) {
		$this->base = $base;
		$this->db = $base->db;
	}

	function get_word_list($start=0,$limit=10){
		$wordlist = array();
		$query = $this->db->query("SELECT * FROM ".DB_TABLEPRE."word ORDER BY id ASC LIMIT $start,$limit ");
		while($word = $this->db->fetch_array($query)){
			$wordlist[] = $word;
		}
		return $wordlist;
	}

	function get_word_num(){
		return $this->db->result_first("SELECT COUNT(*) AS num FROM `".DB_TABLEPRE."word`");
	}
	
	function add_word($find,$replacement,$username){
		$username = addslashes(htmlspecialchars($username));
		$sql = "SELECT id FROM `".DB_TABLEPRE."word` WHERE find = '$find' LIMIT 1";
		if(is_array($this->db->fetch_first($sql))){
			return false;
		}else{
			if(!$replacement){$replacement = "*";}
			$this->db->query("INSERT INTO `".DB_TABLEPRE."word` (`admin`,`find`,`replacement`) VALUES ('$username','$find','$replacement')");
			return true;
		}
	}
	
	function del_words($id){
		if(is_array($id)){
			$ids=implode(',',$id);
		}else{
			$ids=$id;
		}
		$this->db->query("DELETE FROM `".DB_TABLEPRE."word` WHERE id IN ($ids)");
	}
	
	function edit_words($words,$username){
		if(is_array($words)){
			$username = addslashes(htmlspecialchars($username));
			foreach($words as $word){
				$word['find'] = addslashes(htmlspecialchars($word['find']));
				$word['replacement'] = addslashes(htmlspecialchars($word['replacement']));
				$sql = "SELECT * FROM `".DB_TABLEPRE."word` WHERE find = '$word[find]' AND replacement = '$word[replacement]'";
				if(!is_array($this->db->fetch_first($sql))){
					$sql = "UPDATE `".DB_TABLEPRE."word` SET find = '$word[find]',replacement = '$word[replacement]',admin = '$username' WHERE id = '$word[id]'";
					$this->db->query($sql);
				}
			}
			return true;
		}else {
			return false;
		}
	}
}
?>